<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\BaseController;
use App\Models\UserWithdraw;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class WithdrawController extends BaseController
{
    public function index(Request $request)
    {
        $param = $this->validate($request,[
            'status' => 'in:1,2,3,4',
        ]);

        $user_id = auth('api')->id();
        $model = UserWithdraw::query()
                    ->where('user_id',$user_id);

        if (isset($param['status'])){
            $model->where('status',$param['status']);
        }

        return $this->success($model->simplePaginate());
    }


    public function store(Request $request)
    {
        $param = $this->validate($request,[
            'withdraw_amount' => 'required',
            'type' => 'required|in:1,2,3',
            'account_holder' => 'required_if:type,1',
            'bank_of_deposit' => 'required_if:type,1',
            'open_an_account' => 'required_if:type,1',
            'alipay_name' => 'required_if:type,2',
            'alipay_account' => 'required_if:type,2',
        ]);


        $user_id = auth('api')->id();
        $user = DB::table('users')->where('id',$user_id)->first();
        if ($user->balance<$param['withdraw_amount']){
            return  $this->error('对不起您的余额不足');
        }


        $param['user_id'] = $user_id;
        if ($param['type']==1){
            $param['withdraw_info'] = [
                'account_holder' => $param['account_holder'],
                'bank_of_deposit' => $param['bank_of_deposit'],
                'open_an_account' => $param['open_an_account'],
            ];
            DB::table('users')->where('id',$user_id)->update([
                'bank_info' => json_encode([
                    'account_holder' => $param['account_holder'],
                    'bank_of_deposit' => $param['bank_of_deposit'],
                    'open_an_account' => $param['open_an_account'],
                ])
            ]);
        }

        if ($param['type']==2){
            $param['withdraw_info'] = [
                'alipay_name' => $param['alipay_name'],
                'alipay_account' => $param['alipay_account'],
            ];

            DB::table('users')->where('id',$user_id)->update([
                'alipay_info' => json_encode([
                    'alipay_name' => $param['alipay_name'],
                    'alipay_account' => $param['alipay_account'],
                ])
            ]);
        }

        $param['amount_received'] = $param['withdraw_amount'];
        $model = UserWithdraw::query()->create($param);
        if ($model->getKey()){

            DB::table('users')->where('id',$user_id)->decrement('balance',$param['withdraw_amount']);
            DB::table('user_balance_details')
                ->insert([
                    'user_id' => $user_id,
                    'change_type' =>2,
                    'amount'=> $param['withdraw_amount'],
                    'remark' => '提现扣除',
                    'change_time' => date('Y-m-d H:i:s')
                ]);

            return $this->success('申请成功');
        }

        return  $this->error('申请失败');

    }
}
